package com.ebt.m.users;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.AppInitService;
import com.ebt.m.R;
import com.ebt.m.activity.BaseToolBarActivity;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.commons.utils.ToastUtil;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.proposal_v2.widget.dialog.EBTProgressDialog;
import com.ebt.m.users.bean.CardData;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wanglu on 2018/2/7.
 */

public class CardIntroductionActivity extends BaseToolBarActivity {
    @BindView(R.id.et_introduction)
    EditText etIntroduction;
    @BindView(R.id.tips)
    TextView tips;
    @BindView(R.id.action_save)
    Button actionSave;
    @BindView(R.id.item_root)
    LinearLayout itemRoot;
    private EBTProgressDialog mProgressDialog;
    private String mIntroduction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_carddata_edit_introduction);
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
        setTitle("个人介绍");
        ButterKnife.bind(this);
        mIntroduction = getIntent().getStringExtra("introduction");
        initView();

    }

    private void initView() {
        initProgressDialog();
        actionSave.setEnabled(false);
        if (!TextUtils.isEmpty(mIntroduction)) {
            etIntroduction.setText(mIntroduction);
            tips.setText("还可输入" + (200 - mIntroduction.length()) + "个字");
        }
        etIntroduction.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                long length = 200 - s.toString().length();
                tips.setText("还可输入" + (length) + "个字");
                if (length < 200) {
                    actionSave.setEnabled(true);
                } else {
                    actionSave.setEnabled(false);
                }
            }
        });

    }


    private void initProgressDialog() {
        mProgressDialog = new EBTProgressDialog(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    @OnClick(R.id.action_save)
    public void onViewClicked() {
        mProgressDialog.show();
        EBTAPI ebtapi = AppInitService.getEbtApi();
        CardData cardData = new CardData();
        cardData.setIntroduction(etIntroduction.getText().toString().trim());
        ebtapi.saveCardIntroduction(cardData)
                .compose(RxUtil.rxTransformer(this))
                .subscribe(result -> {
                            mProgressDialog.dismiss();
                            try {
                                JSONObject jsonObject = new JSONObject(result.body().string());
                                long num = jsonObject.getJSONObject("data").getLong("num");
                                if (num > 0) {
                                    ToastUtil.show(CardIntroductionActivity.this, "保存个人介绍成功");
                                    Intent intent = getIntent();
                                    intent.putExtra("introduction", etIntroduction.getText().toString().trim());
                                    setResult(RESULT_OK, intent);
                                    finish();
                                } else {
                                    ToastUtil.show(CardIntroductionActivity.this, "保存个人介绍失败");
                                }
                            } catch (Exception e) {
                                ToastUtil.show(CardIntroductionActivity.this, "保存个人介绍失败");
                            }
                        }, throwable -> {
                            mProgressDialog.dismiss();
                            NetUtils.checkHttpException(this, throwable);
                        }
                );

    }
}
